<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GatewayService
 *
 * @author gaitanoboeraeve
 */
class Service_GatewayService 
{
    public function __construct()
    {
        
    }

    public function _traceToLog()
    {
        $myFile = "testFile.txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        $stringData = "Bobby Bopper\n";
        fwrite($fh, $stringData);
        $stringData = "Tracy Tanner\n";
        fwrite($fh, $stringData);
        fclose($fh);
    }
    
    
/*
 * 
 * ANNOUNCEMENT
 * 
 * 
 */    
    /**
     * @return Ahs_VO_Announcement
     */
    public function getAnnouncementById($flexAnnouncementId)
    {
        $announcementMapper = new Application_Model_AnnouncementMapper();
        $announcement = $announcementMapper->fetchById($flexAnnouncementId);
        
        $announcementVO = $this->_toAnnouncementVO($announcement);
        return $announcementVO;
    }
    
    /**
     * @return array
     */
    public function getAnnouncements()
    {
        $announcementMapper = new Application_Model_AnnouncementMapper();
        $announcement = $announcementMapper->fetchAll();
        
        $announcementVOs = $this->_toAnnouncementVOs($announcement);
        return $announcementVOs;
    }
    
    /**
     * @return array
     */
    public function getAnnouncementsByUserId($flexUserId)
    {
        $announcementMapper = new Application_Model_AnnouncementMapper();
        $announcement = $announcementMapper->fetchByUserId($flexUserId);
        
        $announcementVOs = $this->_toAnnouncementVOs($announcement);
        return $announcementVOs;
    }

    /**
     * @return array
     */
    public function getAnnouncementsByCourseId($flexCourseId)
    {
        $announcementMapper = new Application_Model_AnnouncementMapper();
        $announcement = $announcementMapper->fetchByCourseId($flexCourseId);
        
        $announcementVOs = $this->_toAnnouncementVOs($announcement);
        return $announcementVOs;
    }    
    
    /**
     * @param Application_Model_Announcement $announcement
     * @return VO_Announcement 
     */
    private function _toAnnouncementVO(Application_Model_Announcement $announcement)
    {
        $announcementVO = new VO_Announcement();
        $announcementVO->id         = $announcement->Id;
        $announcementVO->title      = $announcement->Title;
        $announcementVO->text      = $announcement->Text;
        $announcementVO->creationdate      = $announcement->Creationdate;
        
        $announcementCourseId = $announcement->CourseId;
        $courseMapper = new Application_Model_CourseMapper();
        $userMapper = new Application_Model_UserMapper();
        $course =  $courseMapper->fetchById($announcementCourseId);
        $user = $userMapper->fetchById($announcement->AuthorId);
        $announcementVO->course_name = $course->Name;
        $announcementVO->course_id = $course->Id;
        $announcementVO->author_name = $user->Fullname;
        return $announcementVO;
    }
    
    /**
     * @param array $announcement
     * @return array
     */
    private function _toAnnouncementVOs($announcements = array())
    {
        $announcementVOs = array();
        foreach ($announcements as $announcement) {
            $announcementVOs[] = $this->_toAnnouncementVO($announcement);
        }
        
        return $announcementVOs;
    }
    
    
/*
 * 
 * Article
 * 
 * 
 */     
    
    /**
     * @return Ahs_VO_Article
     */
    public function getArticleById($flexArticleId)
    {
        $articleMapper = new Application_Model_ArticleMapper();
        $article = $articleMapper->fetchById($flexArticleId);
        
        $articleVO = $this->_toArticleVO($article);
        return $articleVO;
    }
    
    /**
     * @return array
     */
    public function getArticles()
    {
        $articleMapper = new Application_Model_ArticleMapper();
        $article = $articleMapper->fetchAll();
        
        $articleVOs = $this->_toArticleVOs($article);
        return $articleVOs;
    }
    
    public function getArticlesByChapterId($flexChapterId)
    {
        $articleMapper = new Application_Model_ArticleMapper();
        $article = $articleMapper->fetchByChapterId($flexChapterId);
        
        $articleVOs = $this->_toArticleVOs($article);
        return $articleVOs;
    }

    
    /**
     * @param Application_Model_Article $article
     * @return VO_Article 
     */
    private function _toArticleVO(Application_Model_Article $article)
    {
        $articleVO = new VO_Article();
        $articleVO->id                = $article->Id;
        $articleVO->name              = $article->Name;
        $articleVO->text              = $article->Text;
        $articleVO->creationdate      = $article->Creationdate;
        $articleVO->modifieddate      = $article->Modifieddate;        
        return $articleVO;
    }
    
    /**
     * @param array $article
     * @return array
     */
    private function _toArticleVOs($articles = array())
    {
        $articleVOs = array();
        foreach ($articles as $article) {
            $articleVOs[] = $this->_toArticleVO($article);
        }
        
        return $articleVOs;
    }



/*
 * 
 * BOOK
 * 
 * 
 */         
    /**
     * @return Ahs_VO_Book
     */
    public function getBookById($flexBookId)
    {
        $bookMapper = new Application_Model_BookMapper();
        $book = $bookMapper->fetchById($flexBookId);
        
        $bookVO = $this->_toBookVO($book);
        return $bookVO;
    }
    
    /**
     * @return array
     */
    public function getBooks()
    {
        $bookMapper = new Application_Model_BookMapper();
        
        $bookVOs = $this->_toBookVOs($book);
        return $bookVOs;
    }
    
    /**
     * @return array
     */
    public function getBooksByCourseId($flexCourseId)
    {
        $bookMapper = new Application_Model_BookMapper();
        $book = $bookMapper->fetchByCourseId($flexCourseId);
        
        $bookVOs = $this->_toBookVOs($book);
        return $bookVOs;
    }
    
    /**
     * @return array
     */
    public function getBooksByRecent()
    {
        $bookMapper = new Application_Model_BookMapper();
        $book = $bookMapper->fetchByRecent();
        
        $bookVOs = $this->_toBookVOs($book);
        return $bookVOs;
    }
    
    
    /**
     * @return array
     */
    public function getBooksByUserId($flexUserId)
    {
        $bookMapper = new Application_Model_BookMapper();
        $book = $bookMapper->fetchByUserId($flexUserId);
        
        $bookVOs = $this->_toBookVOs($book);
        return $bookVOs;
    }
    
    
    /**
     * @param Application_Model_Book $book
     * @return VO_Book 
     */
    private function _toBookVO(Application_Model_Book $book)
    {
        $author_id = $book->AuthorId;
        
        $bookVO = new VO_Book();
        $bookVO->id          = $book->Id;
        $bookVO->author_id   = $book->AuthorId;
        $bookVO->title       = $book->Title;
        $bookVO->description = $book->Description;
        $bookVO->author_name = $book->Author->Fullname;
        
        return $bookVO;
    }
    
    /**
     * @param array $book
     * @return array
     */
    private function _toBookVOs($books = array())
    {
        $bookVOs = array();
        foreach ($books as $book) {
            $bookVOs[] = $this->_toBookVO($book);
        }
        
        return $bookVOs;
    }
    
    
    
    
    
/*
 * 
 * CHAPTER
 * 
 * 
 */     
    /**
     * @return Ahs_VO_Chapter
     */
    public function getChapterById($flexChapterId)
    {
        $chapterMapper = new Application_Model_ChapterMapper();
        $chapter = $chapterMapper->fetchById($flexChapterId);
        
        $chapterVO = $this->_toChapterVO($chapter);
        return $chapterVO;
    }
    
    /**
     * @return array
     */
    public function getChapters()
    {
        $chapterMapper = new Application_Model_ChapterMapper();
        $chapter = $chapterMapper->fetchAll();
        
        $chapterVOs = $this->_toChapterVOs($chapter);
        return $chapterVOs;
    }
    
    public function getChaptersByBookId($flexBookId)
    {
        $chapterMapper = new Application_Model_ChapterMapper();
        $chapter = $chapterMapper->fetchByBookId($flexBookId);
        
        $chapterVOs = $this->_toChapterVOs($chapter);
        return $chapterVOs;
    }
    
    /**
     * @param Application_Model_Chapter $chapter
     * @return VO_Chapter 
     */
    private function _toChapterVO(Application_Model_Chapter $chapter)
    {
        $chapterVO = new VO_Chapter();
        $chapterVO->id                = $chapter->Id;
        $chapterVO->name              = $chapter->Name;
        $chapterVO->creationdate      = $chapter->Creationdate;
        $chapterVO->modifieddate      = $chapter->Modifieddate;
        return $chapterVO;
    }
    
    /**
     * @param array $chapter
     * @return array
     */
    private function _toChapterVOs($chapters = array())
    {
        $chapterVOs = array();
        foreach ($chapters as $chapter) {
            $chapterVOs[] = $this->_toChapterVO($chapter);
        }
        
        return $chapterVOs;
    }
     

    
    
/*
 * 
 * COMMENT
 * 
 * 
 */     
    /**
     * @return Ahs_VO_Comment
     */
    public function getComment($flexCommentId)
    {
        $commentMapper = new Application_Model_CommentMapper();
        $comment = $commentMapper->fetchById($flexCommentId);
        
        $commentVO = $this->_toCommentVO($comment);
        return $commentVO;
    }
    
    public function setComment($commentVO)
    {
        
        $comment = new Application_Model_Comment();
        /*
        //OPVANGEN ADHV ARRAY DATA
        $comment->Text = $commentVO["text"];
        $comment->Subject = $commentVO["subject"];
        */
        //OPVANGEN ADHV OBJECT DATA
        $comment->Text = $commentVO->text;
        $comment->Subject = $commentVO->subject;
        
        $commentMapper = new Application_Model_CommentMapper();
        $commentMapper->save($comment);
        
    }
    
    /**
     * @return array
     */
    public function getComments()
    {
        $commentMapper = new Application_Model_CommentMapper();
        $comment = $commentMapper->fetchAll();
        
        $commentVOs = $this->_toCommentVOs($comment);
        return $commentVOs;
    }
    
    /**
     * @return array
     */
    public function getCommentsByUserId($flexUserId)
    {
        $commentMapper = new Application_Model_CommentMapper();
        $comment = $commentMapper->fetchByAuthorId($flexUserId);
        
        $commentVOs = $this->_toCommentVOs($comment);
        return $commentVOs;
    }

    
    /**
     * @param Application_Model_Comment $comment
     * @return VO_Comment 
     */
    private function _toCommentVO(Application_Model_Comment $comment)
    {
        $commentVO = new VO_Comment();
        $commentVO->id         = $comment->Id;
        $commentVO->subject      = $comment->Subject;
        $commentVO->text         = $comment->Text;
        $commentVO->creationdate      = $comment->Creationdate;

        
        return $commentVO;
    }
    
    /**
     * @param array $comment
     * @return array
     */
    private function _toCommentVOs($comments = array())
    {
        $commentVOs = array();
        foreach ($comments as $comment) {
            $commentVOs[] = $this->_toCommentVO($comment);
        }
        
        return $commentVOs;
    }
        
    
    
    
    
/*
 * 
 * COURSES
 * 
 * 
 */    
    /**
     * @return Ahs_VO_Course
     */
    public function getCourseById($flexCourseId)
    {
        $courseMapper = new Application_Model_CourseMapper();
        $course = $courseMapper->fetchById($flexCourseId);
        
        $courseVO = $this->_toCourseVO($course);
        return $courseVO;
    }
    
    /**
     * @return array
     */
    public function getCourses()
    {
        $courseMapper = new Application_Model_CourseMapper();
        $courses = $courseMapper->fetchAll();
        
        $courseVOs = $this->_toCourseVOs($courses);
        return $courseVOs;
    }
    
    public function getCoursesByUserId($user_id)
    {
        $courseMapper = new Application_Model_CourseMapper();
        $courses = $courseMapper->fetchByUserId($user_id);
        
        $courseVOs = $this->_toCourseVOs($courses);
        return $courseVOs;
    }

    
    /**
     * @param Application_Model_Course $course
     * @return VO_Course 
     */
    private function _toCourseVO(Application_Model_Course $course)
    {  
     //COURSE  
        $courseVO = new VO_Course();
        $courseVO->course_id           = $course->Id;
        $courseVO->course_name         = $course->Name;
        $courseVO->course_author_id    = $course->Authorid;
        $courseVO->course_description  = $course->Description;
        
     //STATISTICS
        $announcementMapper = new Application_Model_AnnouncementMapper();
        $bookMapper = new Application_Model_BookMapper();
        $courseMapper = new Application_Model_CourseMapper();
        
        //$announcementCount = $announcementMapper->getCountByCourseId($course->Id);
        //$bookCount = $bookMapper->getCountByCourseId($course->Id);
        //$followerCount = $courseMapper->getCountByCourseId($course->Id);
        
        $courseVO->course_announcementCount = 3;
        $courseVO->course_bookCount = 3;
        $courseVO->course_contributorCount = 3;
        $courseVO->course_followerCount = 3;
        
              
        return $courseVO;
    }
    
    /**
     * @param array $course
     * @return array
     */
    private function _toCourseVOs($courses = array())
    {
        $courseVOs = array();
        foreach ($courses as $course) {
            $courseVOs[] = $this->_toCourseVO($course);
        }
        
        return $courseVOs;
    }
    
    

    
    
/*
 * 
 * MESSAGES
 * 
 * 
 */     
    /**
     * @return Ahs_VO_Message
     */
    public function getMessageById($flexMessageId)
    {
        $messageMapper = new Application_Model_MessageMapper();
        $message = $messageMapper->fetchById($flexMessageId);
        
        $messageVO = $this->_toMessageVO($message);
        
        return $messageVO;
    }
    
    /**
     * @return array
     */
    public function getMessages()
    {
        $messageMapper = new Application_Model_MessageMapper();
        $messages = $messageMapper->fetchAll();
        
        $messageVOs = $this->_toMessageVOs($messages);

        return $messageVOs;
    }
    
    /**
     * @param Application_Model_Message $message
     * @return VO_Message 
     */
    private function _toMessageVO(Application_Model_Message $message)
    {
        $messageVO = new VO_Message();
        $messageVO->id         = $message->Id;
        $messageVO->userto      = $message->Userto;
        $messageVO->userfrom  = $message->Userfrom;
        $messageVO->senddate = $message->Senddate;
        $messageVO->subject  = $message->Subject;
        $messageVO->content = $message->Content;
        
        return $messageVO;
    }
    
    /**
     * @param array $messages
     * @return array
     */
    private function _toMessageVOs($messages = array())
    {
        $messageVOs = array();
        foreach ($messages as $message) {
            $messageVOs[] = $this->_toMessageVO($message);
        }
        
        return $messageVOs;
    }
    
 
 /*
* 
* COMMENT
* 
* 
*/     
/**
 * @return Ahs_VO_Review
 */
public function getReview($flexReviewId)
{
	$reviewMapper = new Application_Model_ReviewMapper();
	$review = $reviewMapper->fetchById($flexReviewId);
	
	$reviewVO = $this->_toReviewVO($review);
	return $reviewVO;
}

public function postReviewByVO($flexVO)
{
    
        $mapper = new Application_Model_ReviewMapper();
        $reviewByVO = new Application_Model_Review();

        $reviewByVO->Subject        = $flexVO->subject;
        $reviewByVO->Text           = $flexVO->text;
        $reviewByVO->AuthorId       = $flexVO->author_id;
        $reviewByVO->BookId         = $flexVO->book_id;

        $mapper->save($reviewByVO);
      
        
        
}

/**
 * @return array
 */
public function getReviews()
{
	$reviewMapper = new Application_Model_ReviewMapper();
	$review = $reviewMapper->fetchAll();
	
	$reviewVOs = $this->_toReviewVOs($review);
	return $reviewVOs;
}

/**
 * @return array
 */
public function getReviewsByUserId($flexUserId)
{
	$reviewMapper = new Application_Model_ReviewMapper();
	$review = $reviewMapper->fetchByAuthorId($flexUserId);
	
	$reviewVOs = $this->_toReviewVOs($review);
	return $reviewVOs;
}

/**
 * @return array
 */
public function getReviewsByBookId($flexBookId)
{
	$reviewMapper = new Application_Model_ReviewMapper();
	$review = $reviewMapper->fetchByBookId($flexBookId);
	
	$reviewVOs = $this->_toReviewVOs($review);
	return $reviewVOs;
}


/**
 * @param Application_Model_Review $review
 * @return VO_Review 
 */
private function _toReviewVO(Application_Model_Review $review)
{
	$reviewVO = new VO_Review();
	$reviewVO->id         = $review->Id;
	$reviewVO->subject      = $review->Subject;
	$reviewVO->text         = $review->Text;
	$reviewVO->creationdate      = $review->Creationdate;
        $reviewVO->author_name  = $review->Author->Fullname;
	
	
	return $reviewVO;
}

/**
 * @param array $review
 * @return array
 */
private function _toReviewVOs($reviews = array())
{
	$reviewVOs = array();
	foreach ($reviews as $review) {
		$reviewVOs[] = $this->_toReviewVO($review);
	}
	
	return $reviewVOs;
}
   
    
    
/*
 * 
 * USERS
 * 
 * 
 */     
    /**
     * @return Ahs_VO_User
     */
    public function getUser()
    {
        $userMapper = new Application_Model_UserMapper();
        $user = $userMapper->fetchById(1); 
        $userVO = $this->_toUserVO($user);
        return $userVO;
    }
    
    /**
     * @return Ahs_VO_User
     */
    public function getUserByEmail($zendUserEmail)
    {
        $userMapper = new Application_Model_UserMapper();
        $user = $userMapper->fetchByEmail($zendUserEmail); 
        $userVO = $this->_toUserVO($user);
        return $userVO;
    }
    
    /**
     * @return array
     */
    public function getUsers()
    {
        $userMapper = new Application_Model_UserMapper();
        $users = $userMapper->fetchAll();
        
        $userVOs = $this->_toUserVOs($users);

        return $userVOs;
    }
    
    /**
     * @param Application_Model_User $user
     * @return VO_User 
     */
    private function _toUserVO(Application_Model_User $user)
    {
       
        $country = new Application_Model_Country();
        $countryMapper = new Application_Model_CountryMapper();
        $country = $countryMapper->fetchById($user->CountryId);
        
        $userVO = new VO_User();
        $userVO->id         = $user->Id;
        $userVO->email      = $user->PrimaryEmail->Email;
        $userVO->password   = $user->Password;
        $userVO->givenname  = $user->Givenname;   
        $userVO->familyname = $user->Familyname;
        $userVO->addressline = $user->Addressline;
        $userVO->postcode = $user->Postcode;
        $userVO->country = $country->Name;
        $userVO->lastlogindate = $user->LastloginDate;
        $userVO->creationdate = $user->CreationDate;
        $userVO->dob = $user->Dob;
        $userVO->bio = $user->Bio;
        $userVO->role = $user->Role->Name;
        
        return $userVO;
        
    }
    
    /**
     * @param array $users
     * @return array
     */
    private function _toUserVOs($users = array())
    {
        $userVOs = array();
        foreach ($users as $user) {
            $userVOs[] = $this->_toUserVO($user);
        }
        
        return $userVOs;
    }
}
?>
